/*
  Losu Copyright Notice
  --------------------
    Losu is an open source programming language project under the MIT license
  that can be used for both academic and commercial purposes. There are no
  fees, no royalties, and no GNU-like "copyright" restrictions. Losu qualifies
  as open source software. However, Losu is not public property, and founder
  'chen-chaochen' retains its copyright.

    Losu has been registered with the National Copyright Administration of the
  People's Republic of China, and adopts the MIT license as the copyright
  licensing contract under which the right holder conditionally licenses its
  reproduction, distribution, and modification rights to an unspecified public.

    If you use Losu, please follow the public MIT agreement or choose to enter
  into a dedicated license agreement with us.

  The MIT LICENSE is as follows
  --------------------
  Copyright  2020 - now  chen-chaochen

    Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the “Software”), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

/*
    This document is part of the Losu Community OSPP - 2024 subject results
  contributed by Dongqiao Huang @ NEUQ, following MIT LICENSE Contributed to
  the Losu Community.

  The MIT LICENSE is as follows
  --------------------
  Copyright 2024 Dongqiao Huang

    Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the “Software”), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
 */

#ifndef HCODEGEN_HH
#define HCODEGEN_HH

#include <cstring>
#include <string>
#include <vector>

namespace LosuPreCompiler
{
namespace CodeGen
{

typedef struct cgOutput cgOutput;
typedef struct cgType_module cgType_module;
typedef struct cgType_submod cgType_submod;
typedef struct cgType_class cgType_class;
typedef struct cgType_object cgType_object;
typedef struct cgType_func cgType_func;
typedef struct cgType_script cgType_script;
typedef struct cgType_number cgType_number;
typedef struct cgType_string cgType_string;

typedef struct cgOutput
{
  struct headFile
  {
    std::string headcmt; // head comment
    std::string declare; // head declare
  } headFile;
  struct srcFile
  {
    std::string headcmt; // head comment
    std::string declare; // head declare (include and ...)
    std::string module;  // module constructor
    std::string submod;  // sub module constructor
    std::string object;  // object constructor
    std::string classs;  // class constructor
    std::string func;    // function constructor
  } srcFile;

  struct mdFile
  {
    std::string headcmt; // head comment
    std::string menu;    // head declare
    std::string text;
  } mdFile;

} cgOutput;

typedef struct cgType_func
{
  std::string comment; // comment
  std::string name;    // function name
} cgType_func;

typedef struct cgType_script
{
  std::string comment; // comment
  std::string name;    // script name
  std::string file;    // script file
} cgType_script;

typedef struct cgType_number
{
  std::string comment; // comment
  std::string name;    // number name
  std::string value;   // number value
} cgType_number;

typedef struct cgType_string
{
  std::string comment; // comment
  std::string name;    // string name
  std::string value;   // string value
} cgType_string;

typedef struct cgType_object
{
  std::string comment;               // comment
  std::string name;                  // class name
  std::vector<cgType_object> subObj; // sub object
  std::vector<cgType_func> subFunc;  // sub function
  std::vector<cgType_number> subNum; // sub number
  std::vector<cgType_string> subStr; // sub string
  std::vector<cgType_class> subCls;  // sub class
} cgType_object;

typedef struct cgType_class
{
  std::string comment;               // comment
  std::string name;                  // class name
  std::vector<cgType_func> subFunc;  // sub function
  std::vector<cgType_number> subNum; // sub number
  std::vector<cgType_string> subStr; // sub string
} cgType_class;

typedef struct cgType_submod
{
  std::string comment;                  // comment
  std::string name;                     // sub module name
  std::vector<cgType_class> subCls;     // sub class
  std::vector<cgType_object> subObj;    // sub object
  std::vector<cgType_func> subFunc;     // sub function
  std::vector<cgType_script> subScript; // sub script
  std::vector<cgType_number> subNum;    // sub number
  std::vector<cgType_string> subStr;    // sub string
} cgType_submod;

typedef struct cgType_module
{
  std::string comment;               // comment
  std::string name;                  // module name
  std::vector<cgType_submod> subMod; // sub modules
} cgType_module;

class hCodegen
{
public:
  hCodegen (std::string fname);                             // constructor
  void addModule (cgType_module module, std::int32_t line); // add module
  void addSubmod (cgType_submod submod, std::vector<cgType_submod> *root,
                  std::int32_t line); // add sub module
  void addClass (cgType_class cls, std::vector<cgType_class> *root,
                 std::int32_t line); // add class
  void addObject (cgType_object obj, std::vector<cgType_object> *root,
                  std::int32_t line); // add object
  void addFunc (cgType_func func, std::vector<cgType_func> *root,
                std::int32_t line); // add function
  void addScript (cgType_script script, std::vector<cgType_script> *root,
                  std::int32_t line); // add script
  void addNumber (cgType_number num, std::vector<cgType_number> *root,
                  std::int32_t line); // add number
  void addString (cgType_string str, std::vector<cgType_string> *root,
                  std::int32_t line); // add string
  cgOutput genCode ();                // generate code

private:
  void p_genModule (cgType_module module); // generate module
  void p_genSmod (cgType_submod submod,
                  std::string pname);                      // generate submod
  void p_genClass (cgType_class cls, std::string pname);   // generate class
  void p_genObject (cgType_object obj, std::string pname); // generate object
  void p_genFunc (cgType_func func, std::string pname);    // generate function
  void p_genSpt (cgType_script spt, std::string pname);    // generate script
  std::string p_getPname (std::string cls, std::string obj,
                          std::string pname); // get parent name

protected:
  cgOutput output;          // output
  cgType_module mainModule; // module main
  std::string fname;        // file name
};

}
}

#endif
